<?php
namespace Admin\Model;
use Think\Model;
class CompanyArticleModel extends Model 
{
	protected $insertFields = array('cat_id','title','summary','article_from','detail','create_time','company_id');
	protected $updateFields = array('id','cat_id','title','summary','article_from','detail','create_time','company_id');
	protected $_validate = array(
		array('cat_id', 'number', '分类ID必须是一个整数！', 2, 'regex', 3),
		array('title', '1,50', '标题的值最长不能超过 50 个字符！', 2, 'length', 3),
		array('summary', '1,50', '简介的值最长不能超过 50 个字符！', 2, 'length', 3),
		array('article_from', '1,50', '文章来源的值最长不能超过 50 个字符！', 2, 'length', 3),
		array('detail', 'require', '文章详情不能为空！', 1, 'regex', 3),
	);
	public function search($pageSize = 10)
	{
		/**************************************** 搜索 ****************************************/
		$where = array();
		if($cat_id = I('get.cat_id'))
			$where['cat_id'] = array('eq', $cat_id);
		if($title = I('get.title'))
			$where['title'] = array('like', "%$title%");
		if($summary = I('get.summary'))
			$where['summary'] = array('like', "%$summary%");
		if($article_from = I('get.article_from'))
			$where['article_from'] = array('like', "%$article_from%");
		if($detail = I('get.detail'))
			$where['detail'] = array('eq', $detail);
		/************************************* 翻页 ****************************************/
		$count = $this->alias('a')->where($where)->count();
		$page = new \Think\Page($count, $pageSize);
		// 配置翻页的样式
		$page->setConfig('prev', '上一页');
		$page->setConfig('next', '下一页');
		$data['page'] = $page->show();
		/************************************** 取数据 ******************************************/
		$data['data'] = $this->alias('a')->where($where)->group('a.id')->limit($page->firstRow.','.$page->listRows)->select();
		return $data;
	}
	// 添加前
	protected function _before_insert(&$data, $option)
	{
		if(isset($_FILES['pic']) && $_FILES['pic']['error'] == 0)
		{
			$ret = uploadOne('pic', 'CompanyArticle', array(
				array(350, 350, 2),
				array(150, 150, 2),
				array(50, 50, 2),
			));
			if($ret['ok'] == 1)
			{
				$data['pic'] = $ret['images'][0];
				$data['big_pic'] = $ret['images'][1];
				$data['mid_pic'] = $ret['images'][2];
				$data['sm_pic'] = $ret['images'][3];
			}
			else 
			{
				$this->error = $ret['error'];
				return FALSE;
			}
            $data['create_time'] = date("Y-m-d H:i:s");
        }
	}
	// 修改前
	protected function _before_update(&$data, $option)
	{
		if(isset($_FILES['pic']) && $_FILES['pic']['error'] == 0)
		{
			$ret = uploadOne('pic', 'CompanyArticle', array(
				array(350, 350, 2),
				array(150, 150, 2),
				array(50, 50, 2),
			));
			if($ret['ok'] == 1)
			{
				$data['pic'] = $ret['images'][0];
				$data['big_pic'] = $ret['images'][1];
				$data['mid_pic'] = $ret['images'][2];
				$data['sm_pic'] = $ret['images'][3];
			}
			else 
			{
				$this->error = $ret['error'];
				return FALSE;
			}
			deleteImage(array(
				I('post.old_pic'),
				I('post.old_big_pic'),
				I('post.old_mid_pic'),
				I('post.old_sm_pic'),
	
			));
		}
	}
	// 删除前
	protected function _before_delete($option)
	{
		if(is_array($option['where']['id']))
		{
			$this->error = '不支持批量删除';
			return FALSE;
		}
		$images = $this->field('pic,big_pic,mid_pic,sm_pic')->find($option['where']['id']);
		deleteImage($images);
	}
	/************************************ 其他方法 ********************************************/
}